package com.tdkj.action.backstage.information;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanType;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanInfoComment;
import com.tdkj.service.common.IBeanTypeService;
import com.tdkj.service.information.IBeanMineService;
public class MineAction  extends BaseAction{
	private long id;
	private String classTypeJSON;
	private String varietyid;
	private String variety;
	private String fullId;
	private String title;
	private String content;
	private Timestamp createtime;
	private BeanBaseInfo mine;
	private IBeanMineService mineService;
	private IBeanTypeService typeService;
	private Map<String,String>prop=new HashMap<String,String>();
	private List<BeanBaseInfo> mines=new ArrayList<BeanBaseInfo>();
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanInfoComment.class);
			}
		}
		return findAll();
	}
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getClassTypeJSON() {
		return classTypeJSON;
	}
	public void setClassTypeJSON(String classTypeJSON) {
		this.classTypeJSON = classTypeJSON;
	}
	public BeanBaseInfo getMine() {
		return mine;
	}
	public void setMine(BeanBaseInfo mine) {
		this.mine = mine;
	}
	public String getVarietyid() {
		return varietyid;
	}
	public void setVarietyid(String varietyid) {
		this.varietyid = varietyid;
	}
	public String getVariety() {
		return variety;
	}
	public void setVariety(String variety) {
		this.variety = variety;
	}
	public Timestamp getCreatetime() {
		return createtime;
	}
	public void setCreatetime(Timestamp createtime) {
		this.createtime = createtime;
	}
	public String getFullId() {
		return fullId;
	}
	public void setFullId(String fullId) {
		this.fullId = fullId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	@Resource
	public void setMineService(IBeanMineService mineService) {
		this.mineService = mineService;
	}
	@Resource
	public void setTypeService(IBeanTypeService typeService) {
		this.typeService = typeService;
	}
	public List<BeanBaseInfo> getMines() {
		return mines;
	}
	public void setMines(List<BeanBaseInfo> mines) {
		this.mines = mines;
	}
	public Map<String, String> getProp() {
		return prop;
	}
	public void setProp(Map<String, String> prop) {
		this.prop = prop;
	}
	public String findAll(){
		return "findAll";
	}
	public String findList(){
		Criteria criteria = this.createCriteria(BeanBaseInfo.class);
		criteria.add(Restrictions.eq("moduletype", "6"));
		criteria.addOrder(Order.desc("moduletype"));// 
		this.queryByPage(criteria, "mineAction!findList.action", 10);
		return "findList";
	}
	public String findRightPage(){
		Criteria criteria = this.createCriteria(BeanBaseInfo.class);
		criteria.add(Restrictions.eq("moduletype", "6"));
		criteria.addOrder(Order.desc("moduletype"));// 
		this.queryByPage(criteria, "mineAction!findList.action", 10);
		return "findRightPage";
	}
	public String findAddPage(){
		assemblyJson();
		return "findAddPage";
	}
	public String findEditPage(){
		assemblyJson();
		mine=mineService.findById(this.getId(),BeanBaseInfo.class);
		return "findEditPage";
	}
	public String findRightAddPage(){
		assemblyJson();
		return "findRightAddPage";
	}
	public String findRightEditPage(){
		assemblyJson();
		mine=mineService.findById(this.getId(),BeanBaseInfo.class);
		return "findRightEditPage";
	}
	public String getTree(){
		return "tree";
	}
	
	public String addMineT(){
		BeanBaseInfo mine=new BeanBaseInfo();
		if(this.getTitle()!=null)mine.setTitle(this.getTitle());
		if(this.getContent()!=null)mine.setContent(this.getContent());
		mine.setClicktime(1);
		mine.setModuletype("6");
		mine.setModulename("矿业资源");
		mine.setColumntype("1");
		mine.setColumnname("矿");
		mine.setStatus("未审核");
		if(this.getCreatetime()!=null)mine.setCreatetime(this.getCreatetime());
		if(this.getVariety()!=null)mine.setVariety(this.getVariety());
		if(this.getVarietyid()!=null)mine.setVarietyid(this.getVarietyid());
		if(this.getProp()!=null){
			mine.setProp(this.getProp());
		}
		mineService.save(mine);
		return findAll();
	}
	public String addMineL(){
		BeanBaseInfo mine=new BeanBaseInfo();
		if(this.getTitle()!=null)mine.setTitle(this.getTitle());
		if(this.getContent()!=null)mine.setContent(this.getContent());
		mine.setClicktime(1);
		mine.setModuletype("6");
		mine.setModulename("矿业资源");
		mine.setColumntype("1");
		mine.setColumnname("矿");
		mine.setStatus("未审核");
		if(this.getCreatetime()!=null)mine.setCreatetime(this.getCreatetime());
		if(this.getVariety()!=null)mine.setVariety(this.getVariety());
		if(this.getVarietyid()!=null)mine.setVarietyid(this.getVarietyid());
		if(this.getProp()!=null){
			mine.setProp(this.getProp());
		}
		mineService.save(mine);
		return findList();
	}
	
	public String editMineT(){
		mine=mineService.findById(this.getId(), BeanBaseInfo.class);
		if(this.getTitle()!=null)mine.setTitle(this.getTitle());
		if(this.getContent()!=null)mine.setContent(this.getContent());
		if(this.getCreatetime()!=null)mine.setCreatetime(this.getCreatetime());
		if(this.getVariety()!=null)mine.setVariety(this.getVariety());
		if(this.getVarietyid()!=null)mine.setVarietyid(this.getVarietyid());
		if(this.getProp()!=null){
			mine.setProp(this.getProp());
		}
		mineService.saveOrUpdate(mine, BeanBaseInfo.class);
		return findAll();
	}
	public String editMineL(){
		mine=mineService.findById(this.getId(), BeanBaseInfo.class);
		if(this.getTitle()!=null)mine.setTitle(this.getTitle());
		if(this.getContent()!=null)mine.setContent(this.getContent());
		if(this.getCreatetime()!=null)mine.setCreatetime(this.getCreatetime());
		if(this.getVariety()!=null)mine.setVariety(this.getVariety());
		if(this.getVarietyid()!=null)mine.setVarietyid(this.getVarietyid());
		if(this.getProp()!=null){
			mine.setProp(this.getProp());
		}
		mineService.saveOrUpdate(mine, BeanBaseInfo.class);
		return findList();
	}
	
	public String deleteMineT(){
		mineService.deleteMine(id);
		return findRightPage();
	}
	public String deleteMineL(){
		mineService.deleteMine(id);
		return findList();
	}
	
	//处理状态
	public String updateStatusT(){
		mineService.updateStatus(this.getId());
		return findRightPage();
	}
	//处理状态
	public String updateStatusL(){
		mineService.updateStatus(this.getId());
		return findList();
	}
	public void assemblyJson(){
		StringBuffer  sb=new StringBuffer("[");
		BeanType bean=typeService.getRoot("AREA_ROOT");
		List<BeanType> list=bean.getChildren();
		for(int i=0;i<list.size();i++){
			sb.append("{");
			String id=list.get(i).getId().toString();
			String name=list.get(i).getName();
			List<BeanType> l1=list.get(i).getChildren();
			sb.append("\"id\":"+"\""+id+"\"");
			sb.append(",\"name\":"+"\""+name+"\"");
			for(int j=0;j<l1.size();j++){
				if(j==0)sb.append(",\"chils\":[");
				sb.append("{");
				String id2=l1.get(j).getId().toString();
				String name2=l1.get(j).getName();
				List<BeanType> l2=l1.get(j).getChildren();
				sb.append("\"id\":"+"\""+id2+"\"");
				sb.append(",\"name\":"+"\""+name2+"\"");
				for(int k=0;k<l2.size();k++){
					if(k==0)sb.append(",\"chils\":[");
					sb.append("{");
					String id3=l2.get(k).getId().toString();
					String name3=l2.get(k).getName();
					sb.append("\"id\":"+"\""+id3+"\"");
					sb.append(",\"name\":"+"\""+name3+"\"");
					if(k==(l2.size()-1)){sb.append("}]");}else{sb.append("},");}
				}
				if(j==(l1.size()-1)){sb.append("}]");}else{sb.append("},");}
			}
			if(i==(list.size()-1)){sb.append("}");}else{sb.append("},");}
		}
		sb.append("]");
		this.setClassTypeJSON(sb.toString());
	}
}
